import router from './index'
import store from '@/store'
import { start, close } from '@/utils/nprogress'

// 白名单
const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  start() // 开启顶部进度条
  if (store.getters.token) {
    // 如果存在token, 登录页自动跳转到首页
    if (to.path === '/login') {
      next('/') //直接到后台首页
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach((to, from, next) => {
  to.path !== '/login' && sessionStorage.setItem('path', to.path)
  close() // 关闭顶部进度条
})
